package mn.slide.laborberlin.adaptor;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import mn.slide.laborberlin.adaptor.EinExtendableAdapter.ViewHolderChild;
import mn.slide.laborberlin.db.DataLayer;
import mn.slide.laborberlin.db.DatabaseManager;
import mn.slide.laborberlin.main.DetialActivity;
import mn.slide.laborberlin.main.R;
import mn.slide.laborberlin.main.UeberSchriftActivity;
import mn.slide.laborberlin.model.EntragData;
import mn.slide.laborberlin.model.UeberBase;
import mn.slide.laborberlin.model.UnterSuchung;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.app.Activity;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class UeberSchriftAdapter extends ArrayAdapter<EntragData> {
	private final List<EntragData> list;
	private final Activity myContext;
	private LayoutInflater inflate;

	public UeberSchriftAdapter(Activity context, List<EntragData> list) {
		super(context, R.layout.list_item_middle, list);
		this.list = list;
		this.myContext = context;
		inflate = context.getLayoutInflater();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		final ViewHolderChild child;
		if (rowView == null) {
			rowView = inflate.inflate(R.layout.list_item_middle, null, true);
			child = new ViewHolderChild();
			child.name = (TextView) rowView.findViewById(R.id.title);
			child.detials = (TextView) rowView.findViewById(R.id.subtitle);
			child.arrow = (ImageView) rowView.findViewById(R.id.chevron);
			rowView.setTag(child);
		} else {
			child = (ViewHolderChild) rowView.getTag();
		}
		final EntragData data = list.get(position);
		if (data instanceof UnterSuchung) {
			final UnterSuchung unter = (UnterSuchung) data;
			rowView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(myContext, DetialActivity.class);
					unter.setHistorySet(DatabaseManager.getInstance()
							.getHistoryWithId());
					Log.v("History", "Added");
					Calendar today = Calendar.getInstance();
					SimpleDateFormat format = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					String verlaufDate = format.format(today.getTime());
					unter.setVerlaufDate(verlaufDate);

					DatabaseManager.getInstance().updateUnter(unter);
					DataLayer.layer.addVerlList(unter);
					DataLayer.layer.setUnter(unter);
					HomeGroupActivity.group.goTo(intent);

				}
			});
			child.arrow.setVisibility(View.INVISIBLE);
		} else {
			child.arrow.setVisibility(View.VISIBLE);
			rowView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(myContext,
							UeberSchriftActivity.class);
					intent.putExtra("ueber", (UeberBase) data);
					HomeGroupActivity.group.goTo(intent);
				}
			});
		}
		child.name.setText(Html.fromHtml(data.getName()));
		if (data.getBemerkungLv() != null) {
			child.detials.setText(Html.fromHtml(data.getBemerkungLv()));
			child.detials.setVisibility(View.VISIBLE);
		} else {

			child.detials.setVisibility(View.GONE);
		}

		return rowView;
	}
}
